package net.kazed.nextaction.context.activity;

import net.kazed.nextaction.R;
import net.kazed.nextaction.database.Task;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MenuItem;

/**
 * Task list sub menu.
 */
public class TaskListDisplayMenu {
    
    private static final String DISPLAY_ITEMS = "displayTaskItems";

    private static final String DISPLAY_PROJECTS = "projects";
    private static final String DISPLAY_SUB_PROJECTS = "subProjects";
    private static final String DISPLAY_TASKS = "tasks";
    private static final String DISPLAY_PROJECTS_AND_TASKS = "projectsAndTasks";
    private static final String DISPLAY_ALL = "all";
    
    private SharedPreferences taskListSettings;

    private String display = DISPLAY_ALL;
    private MenuItem displayProjectItem;
    private MenuItem displaySubProjectItem;
    private MenuItem displayTaskItem;
    private MenuItem displayAllItem;
    
    /**
     * Constructor.
     * @param context Android context.
     */
    public TaskListDisplayMenu(Context context) {
        taskListSettings = context.getSharedPreferences("taskListSettings", Context.MODE_PRIVATE);
        display = taskListSettings.getString(DISPLAY_ITEMS, DISPLAY_ALL);
    }

    /**
     * Create menu items.
     * @param menu Menu.
     */
    public void onCreateOptionsMenu(Menu menu) {
        displayProjectItem = menu.findItem(R.id.menu_display_projects);
        displaySubProjectItem = menu.findItem(R.id.menu_display_sub_projects);
        displayTaskItem = menu.findItem(R.id.menu_display_tasks);
        displayAllItem = menu.findItem(R.id.menu_display_all);
        setDisplayMenuItem();
    }
    
    /**
     * Process menu item selection.
     * @param item Selected item.
     * @return True if display sub menu was selected.
     */
    public boolean onOptionsItemSelected(MenuItem item) {
        boolean displayChanged = false;
        
        switch (item.getItemId()) {
        case R.id.menu_display_projects:
            display = DISPLAY_PROJECTS;
            displayChanged = true;
            break;
        case R.id.menu_display_sub_projects:
            display = DISPLAY_SUB_PROJECTS;
            displayChanged = true;
            break;
        case R.id.menu_display_tasks:
            display = DISPLAY_TASKS;
            displayChanged = true;
            break;
        case R.id.menu_display_all:
            display = DISPLAY_ALL;
            displayChanged = true;
            break;
        case R.id.menu_display_projects_and_tasks:
           display = DISPLAY_PROJECTS_AND_TASKS;
           displayChanged = true;
           break;
        }
        if (displayChanged) {
            setDisplayMenuItem();
            SharedPreferences.Editor editor = taskListSettings.edit();
            editor.putString(DISPLAY_ITEMS, display);
            editor.commit();
        }

        return displayChanged;
     }
     
    /**
     * @return Where clause for task list.
     */
     public String getWhereClause() {
         String where = null;
         if (DISPLAY_PROJECTS.equals(display)) {
             where = Task.Column.PARENT_TASK_ID + " is null and " + Task.Column.NUMBER_OF_SUB_TASKS + " > 0";
         } else if (DISPLAY_SUB_PROJECTS.equals(display)) {
             where = Task.Column.PARENT_TASK_ID + " is not null and " + Task.Column.NUMBER_OF_SUB_TASKS + " > 0";
         } else if (DISPLAY_PROJECTS_AND_TASKS.equals(display)) {
            where = Task.Column.PARENT_TASK_ID + " is null";
         } else if (DISPLAY_TASKS.equals(display)) {
            where = "(" + Task.Column.NUMBER_OF_SUB_TASKS + " = 0 or " + Task.Column.NUMBER_OF_SUB_TASKS + " is null)";
         } else {
             where = null;
         }
         return where;
     }

     /**
      * Set the currently selected menu item.
      */
     private void setDisplayMenuItem() {
         if (DISPLAY_PROJECTS.equals(display)) {
             displayProjectItem.setChecked(true);
         } else if (DISPLAY_SUB_PROJECTS.equals(display)) {
                 displaySubProjectItem.setChecked(true);
         } else if (DISPLAY_TASKS.equals(display)) {
             displayTaskItem.setChecked(true);
         } else {
             displayAllItem.setChecked(true);
         }
     }

}
